class Solution {
public:
    int countSubstrings(string s) {
        //dp[i][j]表示子串i到j是回文子串
        //1.s[i]!=s[j]    ->false
        //2.s[i]==s[j]
        //i==j     一个字符     ->true
        //i+1==j   两个字符     ->true
        //i+1<j    有中间串，要保证中间子串也是回文串    dp[i+1][j-1]==true ->true
        int N=s.length();
        vector<vector<bool>>dp(N,vector<bool>(N));
        int ans=0;
        for(int i=N-1;i>=0;--i)
        {
            for(int j=i;j<N;++j)
            {
                if(s[i]==s[j])
                {
                    dp[i][j]=i+1<j?dp[i+1][j-1]:true;
                }
                if(dp[i][j])
                {
                    ans++;
                }
            }
        }
        return ans;
    }
};